# This Python file uses the following encoding: utf-8
# -*- coding: utf-8 -*-
import re
import sys
import time
import datetime
import logging
import  json
import copy
from elasticsearch import Elasticsearch
# 10.15.212.31  online
#10.10.12.92 操作日志线上
# logOnline='10.10.12.92'

reload(sys)
sys.setdefaultencoding('utf8')

readingOnline='10.15.212.31'
readingOnlineBackup='lego-indexServer-web-online002-bjlt.qiyi.virtual'
es = Elasticsearch(hosts=readingOnline)


dslfilterBook={
    'filter':{
        "bool": {
            "must": [
                # {
                #     "terms": {
                #         "title": [
                #             "【一】",
                #             "标题"
                #         ]
                #     }
                # },
                # {
                #     "range": {
                #         "operateTime": {
                #             "ge": "2016-08-03 21:00:00",
                #             # "le":"2016-07-18 14:00:00"
                #         }
                #     }
                # },
                # {
                #     "term":{"status":-1}
                # },
                # {
                #     "term":{"entityTypeNew":14}
                # },
                # {
                #     "term":{"act":3}
                # },
                # {
                #     "term":{"publishStatus":1}
                # },
                # {
                #     "term":{"cpId":1}
                # }
            ],
            "must_not":[
                # {
                #     "term":{"produceStatus":1}
                # },
                # {
                #     "term":{"produceStatus":2}
                # },
            ]
        }
    }
}
dslfilterVolume={
    'filter':{
        "bool": {
            "must": [
                # {
                #     "terms": {
                #         "title": [
                #             "【一】",
                #             "标题"
                #         ]
                #     }
                # },
                {
                    "range": {
                        "operateTime": {
                            "ge": "2016-08-03 21:00:00",
                            # "le":"2016-07-18 14:00:00"
                        }
                    }
                },
                # {
                #     "term":{"status":-1}
                # },
                {
                    "term":{"entityTypeNew":14}
                },
                # {
                #     "term":{"act":3}
                # },
                # {
                #     "term":{"publishStatus":1}
                # },
                # {
                #     "term":{"cpId":1}
                # }
            ],
            "must_not":[
                # {
                #     "term":{"produceStatus":1}
                # },
                # {
                #     "term":{"produceStatus":2}
                # },
            ]
        }
    }
}
dslfilterChapter={
    'filter':{
        "bool": {
            "must": [
                # {
                #     "terms": {
                #         "title": [
                #             "【一】",
                #             "标题"
                #         ]
                #     }
                # },
                {
                    "range": {
                        "createTime": {
                            # "ge": "2016-06-28 00:00:00",
                            "le":"2016-09-18 00:01:00"
                        }
                    }
                },
                # {
                #     "term":{"produceStatus":1}
                # },
                # {
                #     "term":{"cpId":46}
                # },
                {
                    "term":{"status":-1}
                },
                {
                    "term":{"publishStatus":1}# 0为未上线，1为上线，2为下线
                },

            ],
            "must_not":[
                # {
                #     "term":{"produceStatus":1}
                # },
                # {
                #     "term":{"produceStatus":2}
                # },
            ]
        }
    }
}
# filter+query 查询 结构：{query:{filtered：{}}}  好处：借助于filter的速度可以快速过滤出文档，然后再由query根据条件来匹配。
dslfilterQuery={
    "query": {
        "filtered": {
            "query": {
                "match": {
                    "title": "第2章"
                }
            },
            "filter": {
                "term": {
                    "title": "第2章"
                }
            }
        }
    }
}
# query 会根据近义词，相似性等给出相关性分数，并按相关性排序， 适合全文搜索这种本身没有准确答案的领域
dslQuery={
    "query": {
        "bool": {
            "must": [
                {
                    "terms": {
                        "title": [
                            "【一】",
                            "标题"
                        ]
                    }
                },
                {
                    "range": {
                        "createTime": {
                            "gt": "2015-11-09T11:00:00"
                        }
                    }
                }
            ]
        }
    }
}
# es find u'中文' 发
index_name="index_id_book_1445234770866_20151019140610882"
index_type_book="book"
index_type_volume="volume"
index_type_chapter="chapter"
index_type_log="videooperateinfo";
# esRet=es.get(index=index_name, doc_type=index_type_book, id=7505661)['_source']
# print(json.dumps(esRet))
index_name="_all";

def indexSearch(docType,_from=0,size=1,dsl=None):
    '''
    查询一页数据
    :param _from: 偏移
    :param size: 步进
    :param dsl:查询参数
    :return:
    '''
    esRet=es.search(
        index="_all",
        doc_type=docType,
        body=dsl,
        from_=_from,
        size=size,
        # explain=1,
        # fields=['cpId','bookId','qipuId',"id","produceStatus"],
        sort=["id:asc"]
        # sort=["videoId:desc","bookId:asc"]
    )
    return esRet;

def searchMatchAll(docType, queryCondition, pageSize=2000, file=None, *fields):
    '''
    查询匹配过滤条件的所有索引数据
    循环 直到查询不到
    :return:
    '''
    circleCount=0;#循环次数
    recordCount=0;#查询记录数
    file = file
    while(1):
        esRet=indexSearch(docType, circleCount * pageSize, pageSize, queryCondition)
        hitList=esRet['hits']['hits'];
        if(len(hitList)<=0):#循环获取结束
            break;
        for hit in hitList:# 处理一次查询中hits对象下对应的hits数组
            # fieldDict=hit["fields"];#一条记录所对应的fields对象
            fieldDict=hit["_source"];
            # outData=str(fieldDict["qipuId"])+"  "+str(fieldDict["id"])+"  "+\
            #         str(fieldDict["chapterNumber"])+"  "+str(fieldDict["status"])+"  "+\
            #         str(fieldDict["publishStatus"])+"  "+str(fieldDict["title"])+"\n";
            outData="";
            for field in fields:
                outData=outData+str(fieldDict[field])+"  ";
            outData=outData+"\n"
            if(file==None):
                print(outData)
            else:
                file.write(outData)
            recordCount=recordCount+1;
        circleCount=circleCount+1;
    print("循环"+str(circleCount)+" ,处理记录："+str(recordCount))
    print("查询完毕")
    # file.close();



def customCircle():
    '''
    从书的维度查满足条件的章
    :return:
    '''
    file = open ( 'fileout.txt', 'w' )
    for line in open("test1.txt"):
        dsl=copy.deepcopy(dslfilterBook)
        dsl.get("filter").get("bool").get("must").append({"term":{"id":line.strip()}})
        # 书字段
        fieldDict=indexSearch(index_type_book,size=10,dsl=dsl)['hits']['hits'][0]['_source']
        outData=">>>>>>>>>>"+str(fieldDict["qipuId"])+" "+str(fieldDict["title"])+"\n";
        file.write(outData)
        # 循环查询书下的章
        chapterDsl=copy.deepcopy(dslfilterChapter)
        chapterDsl.get("filter").get("bool").get("must").append({"term":{"bookId":line.strip()}})
        searchMatchAll(index_type_chapter,chapterDsl,file=file)
        file.write("\n\n")
    file.close()



# customCircle()

# 查询一次
print(json.dumps(indexSearch(index_type_chapter,size=10,dsl=dslfilterChapter),indent=4))
# 查询所有
# file = open ( 'fileout.txt', 'w' )
# searchMatchAll(index_type_chapter,dslfilterChapter,2000,file,*("id","qipuId","bookId","publishStatus"))